package com.ezia.coreservices.charging.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ezia.coreservices.charging.dao.MaChargeDetailDao;
import com.ezia.coreservices.charging.dao.MaOrderDao;
import com.ezia.coreservices.charging.entity.MaChargeDetailEntity;
import com.ezia.coreservices.charging.entity.MaOrderInfoEntity;
import com.ezia.coreservices.charging.service.MaOrderService;
//import com.ezia.coreservices.charging.utils.PageUtils;

import net.sf.json.JSONObject;


@Service
public class MaOrderServiceImpl implements MaOrderService{
	
	private static final Logger logger = LoggerFactory.getLogger(MaOrderService.class);

	
	@Autowired
	private MaOrderDao maorderdao;
	@Autowired
	private MaChargeDetailDao machargedetaildao; 

	@Override
	public Map<String,Object> getChargeBill(String orderId) {
		Map<String,Object> maOrderList = maorderdao.getChargeBill(orderId);
		int  chargeDetailTotal = machargedetaildao.getCountByOrderId(orderId);
		List<MaChargeDetailEntity> maChargeList = machargedetaildao.findByOrderId(orderId);
		JSONObject majson = JSONObject.fromObject(maOrderList);
		logger.info(String.valueOf(chargeDetailTotal));
		if(chargeDetailTotal == 0) { 
			return maOrderList;
		}else {
			majson.put("ChargeDetails", maChargeList);
			return majson;
		}
	}

	@Override
	public Map<String, Object> getOrderInfo(String orderId) {
		Map<String,Object> maOrderList = maorderdao.getOrderInfo(orderId);
		int  chargeDetailTotal = machargedetaildao.getCountByOrderId(orderId);
		List<MaChargeDetailEntity> maChargeList = machargedetaildao.findByOrderId(orderId);
		JSONObject majson = JSONObject.fromObject(maOrderList);
		logger.info(String.valueOf(chargeDetailTotal));
		if(chargeDetailTotal == 0) {
			return maOrderList;
		}else {
			majson.put("ChargeDetails", maChargeList);
			return majson;
		}
	}

	@Override
	public Object getOrderHistoryInfo(String StartTime, String EndTime, String vin, int offset,
			int pageSize,int pageNo) {
		JSONObject paramsDate = new JSONObject();
		paramsDate.put("StartTime", StartTime);
		paramsDate.put("EndTime", EndTime);
		paramsDate.put("vin", vin);
		paramsDate.put("offset", offset);
		paramsDate.put("pageSize", pageSize);
		
		List<Map<String,Object>> maOrderList = maorderdao.getOrderHistoryInfo(paramsDate);
		Map<String,Object> maOrderCountList = maorderdao.getOrderHistoryCountInfo(StartTime, EndTime, vin);
		Object orderCount = maOrderCountList.get("Count");
		double Count=Double.valueOf(orderCount.toString());
		int pageCount = (int)Math.ceil((double)Count/pageSize);
		JSONObject majson = JSONObject.fromObject(maOrderCountList);
		majson.put("pageNo", pageNo);
		majson.put("pageCount", pageCount);
		majson.put("orderList", maOrderList);
		return majson;
	}


}
